package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.AddBreedingPairInfoResponse
import com.gexiaobao.pigeon.app.model.bean.AddBreedingPairSubResponse
import com.gexiaobao.pigeon.app.model.bean.BloodBookDetailResponse
import com.gexiaobao.pigeon.app.model.bean.BloodBookHouseResponse
import com.gexiaobao.pigeon.app.model.bean.BloodBookListBean
import com.gexiaobao.pigeon.app.model.bean.BloodLineDictionaryResponse
import com.gexiaobao.pigeon.app.model.bean.BloodPigeonListBean
import com.gexiaobao.pigeon.app.model.bean.BloodPigeonPublicResponse
import com.gexiaobao.pigeon.app.model.bean.BreedCategoryResponse
import com.gexiaobao.pigeon.app.model.bean.BreedStatusResponse
import com.gexiaobao.pigeon.app.model.bean.BreedingPairList
import com.gexiaobao.pigeon.app.model.bean.BreedingPairSubList
import com.gexiaobao.pigeon.app.model.bean.BreedingPigeonDetailResponse
import com.gexiaobao.pigeon.app.model.bean.BreedingRecordResponse
import com.gexiaobao.pigeon.app.model.bean.DefaultResponse
import com.gexiaobao.pigeon.app.model.bean.GetBreedingPairSubInfoResponse
import com.gexiaobao.pigeon.app.model.bean.GetBreedingPairTimeAxisInfoResponse
import com.gexiaobao.pigeon.app.model.bean.PgBloodResponse
import com.gexiaobao.pigeon.app.model.bean.PgEyeResponse
import com.gexiaobao.pigeon.app.model.bean.PgFeatherColorResponse
import com.gexiaobao.pigeon.app.model.bean.PgSourceResponse
import com.gexiaobao.pigeon.app.model.bean.PgYearResponse
import com.gexiaobao.pigeon.app.model.bean.PublicBreedingPigeonListBean
import com.gexiaobao.pigeon.app.model.bean.ReminderResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.UpLoadBookImageResponse
import com.gexiaobao.pigeon.app.model.bean.UploadBloodImageResponse
import com.gexiaobao.pigeon.app.model.param.AddBloodPigeonParam
import com.gexiaobao.pigeon.app.model.param.EditBloodHoseParam
import com.gexiaobao.pigeon.app.model.param.MakePedigreeBookParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.SettingUtil
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * @Author      : hxw
 * @Date        : 2024/8/16 13:33
 * @Describe    :
 */
class BreedingPigeonViewModel : BaseViewModel() {

    private var pageNo = 1
    private var pageSize = 20

    /** 添加血统书图片 */
    val addBloodImageResult: MutableLiveData<Any> = MutableLiveData()
    fun addBloodBookImg(body: RequestBody) {
        request({ apiService.addBloodBookImg(body) }, {
            addBloodImageResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 修改鸽舍信息 */
    val editHouseResult: MutableLiveData<Any> = MutableLiveData()
    fun editBloodBookHouse(param: EditBloodHoseParam) {
        request({ apiService.editBloodBookHouse(param) }, {
            editHouseResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 获取鸽舍信息 */
    val getBloodBookHouseResult: MutableLiveData<BloodBookHouseResponse> = MutableLiveData()
    fun getBloodBookHouse(id: String) {
        request({ apiService.getBloodBookHouse(id) }, {
            getBloodBookHouseResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 删除血统书 */
    val delBloodBookResult: MutableLiveData<Any> = MutableLiveData()
    fun delBloodBook(id: String) {
        request({ apiService.delBloodBook(id) }, {
            delBloodBookResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 获取血统书列表 */
    val getBloodBookResult: MutableLiveData<ListDataUiState<BloodBookListBean>> = MutableLiveData()
    fun getBloodBookList(isRefresh: Boolean, query: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getBloodBookList(pageNo, pageSize, query) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            getBloodBookResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<BloodBookListBean>()
            )
            getBloodBookResult.value = listDataUiState
        })
    }

    /** 获取血统书详情 */
    val getBloodBookInfoResult: MutableLiveData<ResultState<BloodBookDetailResponse>> = MutableLiveData()
    fun getBloodBookInfo(id: String) {
        request({ apiService.getBloodBookInfo(id) }, getBloodBookInfoResult)
    }

    /** 获取血统书详情 */
    fun getBloodBookInfoByPigeonId(pigeonId: String) {
        request({ apiService.getBloodBookInfoByPigeonId(pigeonId) }, getBloodBookInfoResult)
    }

    /** 获取血统书详情(根据memberIdAndRingId) */
    fun getBloodBook(param: RequestBody) {
        request({ apiService.getBloodBook(param) }, getBloodBookInfoResult)
    }

    /** 新增血统书 */
    val addBloodBookResult: MutableLiveData<DefaultResponse> = MutableLiveData()
    fun addBloodBook(param: MakePedigreeBookParam) {
        request({ apiService.addBloodBook(param) }, {
            addBloodBookResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 修改血统书 */
    val editBloodBookResult: MutableLiveData<Any> = MutableLiveData()
    fun editBloodBook(param: MakePedigreeBookParam) {
        request({ apiService.editBloodBook(param) }, {
            editBloodBookResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 修改作育配对编号 */
    val editBreedingPairNumResult: MutableLiveData<Any> = MutableLiveData()
    fun editBreedingPairInfo(id: Int, pairNum: String) {
        request({ apiService.editBreedingPairInfo(id, pairNum) }, {
            editBreedingPairNumResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 编辑作育提醒 */
    val editBreedingPairResult: MutableLiveData<Any> = MutableLiveData()
    fun editBreedingPairSettingToApp(body: RequestBody) {
        request({ apiService.editBreedingPairSettingToApp(body) }, {
            editBreedingPairResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 删除作育记录信息 */
    val delBreedingPairSubInfoResult: MutableLiveData<Any> = MutableLiveData()
    fun delBreedingPairSubInfo(id: String) {
        request({ apiService.delBreedingPairSubInfo(id) }, {
            delBreedingPairSubInfoResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 获取作育提醒设置 */
    val reminderResult: MutableLiveData<ReminderResponse> = MutableLiveData()
    fun getBreedingPairSetting() {
        request({ apiService.getBreedingPairSetting() }, {
            reminderResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    val editTimeAxisResult: MutableLiveData<Any> = MutableLiveData()
    fun editBreedingPairTimeAxisInfo(id: Int, date: String, status: Int, ringOne: String, ringTwo: String, remark: String) {
        request({ apiService.editBreedingPairTimeAxisInfo(id, date, status, ringOne, ringTwo, remark) }, {
            editTimeAxisResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 获取作育跟踪 */
    val getBreedingPairSubInfoResult: MutableLiveData<GetBreedingPairSubInfoResponse> = MutableLiveData()
    fun getBreedingPairSubInfo(id: Int) {
        request({ apiService.getBreedingPairSubInfo(id) }, {
            getBreedingPairSubInfoResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 修改作育记录信息 */
    val editBreedingPairSubInfoResult: MutableLiveData<Any> = MutableLiveData()
    fun editBreedingPairSubInfo(
        id: Int,
        pairTime: String,
        nestBox: String,
        femaleNanny: String,
        maleNanny: String,
        nannyNestBox: String,
        remark: String
    ) {
        request({ apiService.editBreedingPairSubInfo(id, pairTime, nestBox, femaleNanny, maleNanny, nannyNestBox, remark) }, {
            editBreedingPairSubInfoResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 获取作育跟踪时间轴 */
    val getBreedingPairTimeAxisResult: MutableLiveData<GetBreedingPairTimeAxisInfoResponse> = MutableLiveData()
    fun getBreedingPairTimeAxisInfo(id: String) {
        request({ apiService.getBreedingPairTimeAxisInfo(id) }, {
            getBreedingPairTimeAxisResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 保存作育跟踪 */
    val addBreedingPairSubResult: MutableLiveData<AddBreedingPairSubResponse> = MutableLiveData()
    fun addBreedingPairSubInfo(body: RequestBody) {
        request({ apiService.addBreedingPairSubInfo(body) }, {
            addBreedingPairSubResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 获取作育记录列表 */
    val getBreedingPairSubDataResult: MutableLiveData<BreedingRecordResponse> = MutableLiveData()
    fun getBreedingPairSub(breedingPairId: Int) {
        request({ apiService.getBreedingPairSub(breedingPairId, 1, 0) }, {
            getBreedingPairSubDataResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 删除作育配对 */
    val delBreedingPairInfoResult: MutableLiveData<Any> = MutableLiveData()
    fun delBreedingPairInfo(id: String) {
        request({ apiService.delBreedingPairInfo(id) }, {
            delBreedingPairInfoResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 获取作育记录列表 */
    val getBreedingPairSubResult: MutableLiveData<ListDataUiState<BreedingPairSubList>> = MutableLiveData()
    fun getBreedingPairSubList(isRefresh: Boolean, breedingPairId: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getBreedingPairSubList(breedingPairId, pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            getBreedingPairSubResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<BreedingPairSubList>()
            )
            getBreedingPairSubResult.value = listDataUiState
        })
    }

    /** 获取作育配对列表 */
    val getBreedingPairLstResult: MutableLiveData<ListDataUiState<BreedingPairList>> = MutableLiveData()
    fun getBreedingPairLst(isRefresh: Boolean, query: String, id: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getBreedingPairLst(pageNo, pageSize, query, id) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            getBreedingPairLstResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<BreedingPairList>()
            )
            getBreedingPairLstResult.value = listDataUiState
        })
    }

    /** 作育配对 */
    val addBreedingPairInfoResult: MutableLiveData<AddBreedingPairInfoResponse> = MutableLiveData()
    fun addBreedingPairInfo(body: RequestBody) {
        request({ apiService.addBreedingPairInfo(body) }, {
            addBreedingPairInfoResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 上传图片(单张) */
    val upLoadBookImageResult: MutableLiveData<UpLoadBookImageResponse> = MutableLiveData()
    fun uploadGenealogyBookImg(imageFile: File) {
        val body: RequestBody = imageFile.asRequestBody("multipart/form-data".toMediaTypeOrNull()) //表单类型
        val builder = MultipartBody.Builder().setType(MultipartBody.FORM)//表单类型
        builder.addFormDataPart("img", imageFile.name, body) //添加图片数据，body创建的请求体
        val requestBody = builder.build()
        request({ apiService.uploadGenealogyBookImg(requestBody) }, {
            upLoadBookImageResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 上传图片(多张) */
    val uploadImagesResult: MutableLiveData<UploadBloodImageResponse> = MutableLiveData()
    fun uploadGenealogyBookImg(imageFile: HashMap<String, ArrayList<File>>) {
        val body = SettingUtil.fileImageListToMultipartBody(imageFile)
        request({ apiService.uploadGenealogyBookImgs(body) }, {
            uploadImagesResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }

    /** 删除种鸽信息 */
    val deletePigeonResult: MutableLiveData<UiState> = MutableLiveData()
    fun delBreedingPigeonInfo(id: String) {
        request({ apiService.delBreedingPigeonInfo(id) }, {
            val result = UiState(isSuccess = true)
            deletePigeonResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            deletePigeonResult.value = result
        })
    }

    /** 获取种鸽详情 */
    val detailResult: MutableLiveData<BreedingPigeonDetailResponse> = MutableLiveData()
    fun getBreedingPigeonInfo(id: String) {
        request({ apiService.getBreedingPigeonInfo(id) }, {
            detailResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 添加种鸽 */
    val addBreedingPigeonResult: MutableLiveData<UiState> = MutableLiveData()
    fun addBreedingPigeon(param: AddBloodPigeonParam) {
        val imageList = param.img
        val fileList = HashMap<String, ArrayList<File>>()
        val hashMap = HashMap<String, String>()

        hashMap["country"] = param.country
        hashMap["countryEn"] = param.countryEn
        hashMap["year"] = param.year
        hashMap["area"] = param.area
        hashMap["ringId"] = param.ringId
        hashMap["subRing"] = param.subRing
        hashMap["name"] = param.name
        hashMap["status"] = param.status
        hashMap["category"] = param.category
        hashMap["sex"] = param.sex
        hashMap["hatchedDate"] = param.hatchedDate
        hashMap["featherColor"] = param.featherColor
        hashMap["eye"] = param.eye
        hashMap["blood"] = param.blood
        hashMap["source"] = param.source
        hashMap["pgDesc"] = param.pgDesc
        hashMap["gradesDesc"] = param.gradesDesc
        hashMap["photoFlag"] = param.photoFlag.toString()

        fileList["img"] = imageList

        val body = SettingUtil.fileListToMultipartBody(fileList, hashMap)
        request({ apiService.addBreedingPigeon(body) }, {
            val deleteState = UiState(isSuccess = true)
            addBreedingPigeonResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            addBreedingPigeonResult.value = deleteState
        })
    }

    /** 编辑种鸽 */
    val editBreedingPigeonInfoResult: MutableLiveData<UiState> = MutableLiveData()
    fun editBreedingPigeonInfo(param: RequestBody) {
        request({ apiService.editBreedingPigeonInfo(param) }, {
            val deleteState = UiState(isSuccess = true)
            editBreedingPigeonInfoResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            editBreedingPigeonInfoResult.value = deleteState
        })
    }

    /** 获取种鸽列表 */
    val getPigeonResult: MutableLiveData<ListDataUiState<BloodPigeonListBean>> = MutableLiveData()
    fun getBreedingPigeonList(
        isRefresh: Boolean,
        status: String,
        category: String,
        sex: String,
        year: String,
        ringOrName: String,
        featherColor: String,
        eye: String,
        blood: String,
    ) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getBreedingPigeonList(pageNo, pageSize, status, category, sex, year, ringOrName, featherColor, eye, blood) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            getPigeonResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<BloodPigeonListBean>()
            )
            getPigeonResult.value = listDataUiState
        })
    }

    /** 获取种鸽列表(公库) */
    val getPigeonPublicResult: MutableLiveData<ListDataUiState<PublicBreedingPigeonListBean>> = MutableLiveData()
    fun getBreedingPigeonList(isRefresh: Boolean, ringOrName: String, sex: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getPublicBloodPigeon(pageNo, pageSize, ringOrName, sex) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            getPigeonPublicResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<PublicBreedingPigeonListBean>()
            )
            getPigeonPublicResult.value = listDataUiState
        })
    }

    /** 删除自定义信息 */
    val delCustomInfoResult: MutableLiveData<Any> = MutableLiveData()
    fun delCustomInfo(id: Int, type: Int) {
        request({ apiService.delCustomInfo(id, type) }, {
            delCustomInfoResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 编辑自定义信息 */
    val editCustomInfoResult: MutableLiveData<Any> = MutableLiveData()
    fun editCustomInfo(id: Int, info: String, type: Int) {
        request({ apiService.editCustomInfo(id, info, type) }, {
            editCustomInfoResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获取年份列表 */
    val getYearResult: MutableLiveData<PgYearResponse> = MutableLiveData()
    fun getYearList() {
        request({ apiService.getYearList() }, {
            getYearResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获取种鸽来源列表 */
    val getPgSourceResult: MutableLiveData<PgSourceResponse> = MutableLiveData()
    fun getPgSource() {
        request({ apiService.getPgSource() }, {
            getPgSourceResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获取血統列表 */
    val getPgBloodResult: MutableLiveData<PgBloodResponse> = MutableLiveData()
    fun getPgBlood(type: Int) {//1-查所有 2-查自定义
        request({ apiService.getPgBlood(type) }, {
            getPgBloodResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 自定义眼砂 */
    val addPgEyeResult: MutableLiveData<Any> = MutableLiveData()
    fun addPgEye(body: RequestBody) {
        request({ apiService.addPgEye(body) }, {
            addPgEyeResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 自定义羽色 */
    val addPgColorResult: MutableLiveData<Any> = MutableLiveData()
    fun addPgFeatherColor(body: RequestBody) {
        request({ apiService.addPgFeatherColor(body) }, {
            addPgColorResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 自定义血統 */
    val addPgBloodResult: MutableLiveData<Any> = MutableLiveData()
    fun addPgBlood(body: RequestBody) {
        request({ apiService.addPgBlood(body) }, {
            addPgBloodResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获取眼砂列表 */
    val getPgEyeResult: MutableLiveData<PgEyeResponse> = MutableLiveData()
    fun getPgEye(type: Int) {//1-查所有 2-查自定义
        request({ apiService.getPgEye(type) }, {
            getPgEyeResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获取羽色列表 */
    val getPgFeatherColorResult: MutableLiveData<PgFeatherColorResponse> = MutableLiveData()
    fun getPgFeatherColor(type: Int) {//1-查所有 2-查自定义
        request({ apiService.getPgFeatherColor(type) }, {
            getPgFeatherColorResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获获取种鸽类别 */
    val getPgBreedCategoryResult: MutableLiveData<BreedCategoryResponse> = MutableLiveData()
    fun getPgBreedCategory() {
        request({ apiService.getPgBreedCategory() }, {
            getPgBreedCategoryResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获取国家/地区列表 */
    val getBloodLineDictionaryResult: MutableLiveData<BloodLineDictionaryResponse> = MutableLiveData()
    fun getBloodLineDictionary() {
        request({ apiService.getBloodLineDictionary() }, {
            getBloodLineDictionaryResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获取种鸽状态 */
    val getPgBreedStatusResult: MutableLiveData<BreedStatusResponse> = MutableLiveData()
    fun getPgBreedStatus() {
        request({ apiService.getPgBreedStatus() }, {
            getPgBreedStatusResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

}